package com.hhf.feiojcodesandbox.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.ListContainersCmd;
import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.command.PullImageResultCallback;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Optional;

/**
 * @Author hhf
 * @Date 2024/10/30
 * @Description
 */
@Slf4j
public class DockerDemo {

    public static void main(String[] args) {

//        //Docker客户端连接配置
//        DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
//                .withDockerHost("tcp://192.168.140.128:2375")
//                .build();

        //创建Docker客户端
        DockerClient dockerClient = DockerClientBuilder.getInstance().build();

        //查看是否连通
        dockerClient.pingCmd().exec();

//        //拉取镜像
//        String repository = "nginx:latest";
//        PullImageCmd pullImageCmd = dockerClient.pullImageCmd(repository);
//        PullImageResultCallback pullImageResultCallback = new PullImageResultCallback(){
//            @Override
//            public void onNext(PullResponseItem item) {
//                log.info("正在拉取远程镜像:{}",item.getStatus());
//                super.onNext(item);
//            }
//        };
//        try {
//            pullImageCmd.exec(pullImageResultCallback).awaitCompletion();
//            log.info("pull image over");
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        //创建容器
//        String image = "nginx:latest";
//        CreateContainerResponse response = dockerClient.createContainerCmd(image).exec();
//        String containerId = response.getId();
//        log.info("容器{}创建完成",containerId);
//
//        //查看容器状态
//        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
//        List<Container> containerList = listContainersCmd.withShowAll(true).exec();
//        containerList.stream().forEach(System.out::println);
//
//        //启动容器
//        dockerClient.startContainerCmd(containerId).exec();
//        log.info("容器{}启动成功",containerId);
//
//        //查看日志
//        LogContainerResultCallback logContainerResultCallback = new LogContainerResultCallback(){
//            @Override
//            public void onNext(Frame item) {
//                log.info(new String(item.getPayload()));
//                super.onNext(item);
//            }
//        };
//        log.info("开始接收容器{}的日志信息...",containerId);
//        try {
//            dockerClient.logContainerCmd(containerId)
//                    .withStdErr(true)
//                    .withStdOut(true)
//                    .exec(logContainerResultCallback)
//                    .awaitCompletion();
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//
//        //删除容器
//        log.info("开始删除容器{}",containerId);
//        dockerClient.removeContainerCmd(containerId).withForce(true).exec();
//        log.info("容器{}删除成功",containerId);
//
//        //删除镜像
//        dockerClient.removeImageCmd(image).withForce(true).exec();
//        log.info("镜像{}删除成功",image);


    }

}
